package project.active;

import java.sql.SQLException;
import java.util.ArrayList;

import model.pojo.Employee;
import project.adapter.ProjectListErrorAdapter;
import project.dao.TestProjectDAO;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import approve.action.R;

public class ListErrorProjectManager extends Activity{
	TextView currentNameProject,lbListErr,lbStatus,lbdetailerr;
	Spinner cbStatus;
	private String lang="EN",database="admin";
	Button bttAddError;
	PrProject prProject;
	Employee emp = new Employee();
	TestProjectDAO pr = new TestProjectDAO();
	private int status = 0;
	ListView lstError;
	TextView logout,username;
	Bundle language;
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	public String getLang(){
    	Bundle extras = getIntent().getExtras();
    	if(extras != null){
    		lang = extras.getString("lang");
    	}
    	return lang;
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listerrorprojectmanager);
		language = getIntent().getExtras().getBundle(getLang());
		
		currentNameProject = (TextView)findViewById(R.id.valueNameProject);
		lbListErr = (TextView)findViewById(R.id.textIDProject);
//		lbStatus = (TextView)findViewById(R.id.idStatus);
		lbdetailerr = (TextView)findViewById(R.id.lbDetailError);
		
//		initStatus();
		
		Intent intent = this.getIntent();
		prProject = (PrProject)intent.getSerializableExtra("prproject");
		emp =(Employee)intent.getSerializableExtra("empl");
		currentNameProject.setText(prProject.getNameProject());
		lbListErr.setText(language.getString("l_errinfo"));
		
		bttAddError = (Button)findViewById(R.id.addError);
		bttAddError.setOnClickListener(processBttError);
		try {
			lstError = (ListView)findViewById(R.id.listDetailError);
			ProjectListErrorAdapter adapter1 = new ProjectListErrorAdapter(this, loadAllListError());
			lstError.setAdapter(adapter1);
			
//			lstError.setAdapter(new ArrayAdapter<PrTestProject>(this,  android.R.layout.simple_list_item_1, loadAllListError()));
			lstError.setOnItemClickListener(listenerListAllError);
			
			logout=(TextView)findViewById(R.id.logout);
			username=(TextView)findViewById(R.id.username);
			
			logout.setOnClickListener(listenerLogout);
			username.setText(emp.getUserName());
			logout.setText(language.getString("Logout"));

//			report=(TextView)findViewById(R.id.report);report.setText("22");
			bttAddError.setText(language.getString("add_error"));
//			lbStatus.setText(language.getString("ac_Status"));
			lbdetailerr.setText(language.getString("error"));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}
	
	public OnClickListener processBttError = new OnClickListener() {
		
		public void onClick(View v) {
			Intent intent = new Intent(ListErrorProjectManager.this,AddErrorManager.class);
			intent.putExtras(getIntent().getExtras());
		    intent.putExtra("prprojectError", prProject);
		    intent.putExtra("employee", emp);
		    intent.putExtra("database", getDatabase());
		    intent.putExtra("lang", lang);
			
			startActivity(intent);
		}
	};

	private void initStatus(){
		String[] langs = new String[] { "Not fix", "Fix complete", "delete" };
		ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, langs);
//		cbStatus = (Spinner) findViewById(R.id.cbStatus);
		cbStatus.setAdapter(arr);
		cbStatus.setOnItemSelectedListener(selectStatus);
	}
	
	private OnItemSelectedListener selectStatus = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
//			Toast.makeText(parent.getContext(), "The planet is " +
//			          parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
			if(parent.getItemAtPosition(position).toString().equalsIgnoreCase("Not fix")){
				status = 0;
				try {
					reloadThisPage();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else if(parent.getItemAtPosition(position).toString().equalsIgnoreCase("Fix complete")){
				status = 1;
				try{
					reloadThisPage();
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
			else if(parent.getItemAtPosition(position).toString().equalsIgnoreCase("delete")){
				status = 2;
				try {
					reloadThisPage();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		public void onNothingSelected(AdapterView<?> parent) {
			status=0;
		}
	};

		
	private void reloadThisPage() throws SQLException{
		ProjectListErrorAdapter adapterr = new ProjectListErrorAdapter(this, loadAllListError());
		lstError.setAdapter(adapterr);
//		lstError.setAdapter(new ArrayAdapter<PrTestProject>(this, android.R.layout.simple_list_item_1, loadAllListError()));
	}
	
	public  ArrayList<PrTestProject> loadAllListError() throws SQLException {
		ArrayList<PrTestProject> item=new ArrayList<PrTestProject>();
//		item=pr.getListErrorByIDProject(getDatabase(),prProject.getIdProject(),status);
		item = pr.getListErrorByIDProject(getDatabase(), prProject.getIdProject());
		return item;
	}

	private OnItemClickListener listenerListAllError = new OnItemClickListener() {

		
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			try{
				PrTestProject err=(PrTestProject)lstError.getItemAtPosition(arg2);
				Intent i=new Intent(ListErrorProjectManager.this,ProjectManagerErrorDetail.class);
				
				System.out.println("Error status : =====" + err.getStatus());
				i.putExtras(getIntent().getExtras());
				i.putExtra("project", prProject);
				i.putExtra("errorproject", err);
				i.putExtra("database", database);
				i.putExtra("em", emp);
				i.putExtra("lang", lang);
				
				startActivity(i);
			}catch (Exception e) {
				alertbox("Error", e.getMessage());
			}
		}
	};
	protected void alertbox(String title, String mymessage)
	   {
	   new AlertDialog.Builder(this)
	      .setMessage(mymessage)
	      .setTitle(title)
	      .setCancelable(true)
	      .setNeutralButton(android.R.string.cancel,
	         new DialogInterface.OnClickListener() {
	         public void onClick(DialogInterface dialog, int whichButton){}
	         })
	      .show();
	   }
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(ListErrorProjectManager.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
